package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.Notice;
import com.jcf.springboot.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-04-09
 */
@RestController
@RequestMapping("/notice/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("getNoticeList")
    public R getNoticeList(){
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("notice_time");
        List<Notice> list = noticeService.list(wrapper);
        if(list != null){
            return R.ok().data(list).message("获取公告列表成功");
        }
        else {
            return R.error().message("获取公告列表失败");
        }

    }
    @GetMapping("getNoticeById")
    public R getNoticeById(String noticeId){
        Notice notice = noticeService.getById(noticeId);
        if(notice != null){
            return R.ok().data(notice).message("获取公告成功");
        }
        return R.error().message("获取公告失败");
    }

    @PostMapping("saveNotice")
    public R saveNotice(@RequestBody Notice notice){
        boolean save = noticeService.save(notice);
        if(save){
            return R.ok().message("新增公告成功");
        }
        return R.error().message("新增公告失败");
    }

    @PostMapping("editNotice")
    public R editNotice(@RequestBody Notice notice){
        boolean b = noticeService.updateById(notice);
        if(b){
            return R.ok().message("修改公告成功");
        }
        return R.error().message("修改公告失败");
        }

        @DeleteMapping("removeNotice")
        public R removeNotice(String id){
            boolean b = noticeService.removeById(id);
            if(b){
                return R.ok().message("删除公告成功");
            }
            return R.error().message("删除公告失败");
        }
}

